package com.example.text.fg

import android.graphics.Color
import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.TextView
import android.widget.Toast
import com.example.text.R
import com.example.text.base.BaseFragment
import com.example.text.databinding.FragmentMessageBinding
import com.example.text.diyviews.MyFlowLayout
import com.example.text.main.viewmodel.MainViewModel
import com.example.text.utils.ToastUtils
import com.google.android.material.internal.FlowLayout


class MessageFragment : BaseFragment<MainViewModel,FragmentMessageBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_message


    private var mDatas: List<String>? = null

    override fun initView() {
        super.initView()
        loadData();

        staticAddView();

        //动态添加View
        //dynamicAddView();
    }
    private fun staticAddView() {
        val padding10 = 10

        mBinding.flowLayout.setPadding(padding10, padding10, padding10, padding10)



        for (i in mDatas!!.indices) {
            val textView = LayoutInflater.from(requireActivity()).inflate(R.layout.item_search_history, mBinding.flowLayout, false) as TextView
            val s = mDatas!![i]
            textView.text = s
            mBinding.flowLayout.addView(textView)
            textView.setOnClickListener {
                ToastUtils.showShort(requireActivity(),s)
            }
        }

        mBinding.flowLayout.setMaxLines(5) // 设置最大行数

    }

    private fun dynamicAddView() {
        val padding6 = 6
        val padding8 = 8
        val padding10 = 10
        val padding12 = 12

        mBinding.flowLayout.setPadding(padding10, padding10, padding10, padding10)
        mBinding.flowLayout.setHorizontalSpacing(padding8) // 水平间距
        mBinding.flowLayout.setVerticalSpacing(padding10)  // 竖直边距

        for (key in mDatas!!) {
            val textView = TextView(requireActivity()).apply {
                text = key
                setTextColor(Color.parseColor("#333333"))
                textSize =12f
                setPadding(padding12, padding6, padding12, padding6)
                gravity = Gravity.CENTER
                setBackgroundResource(R.drawable.shape_bg_edit_text_search)
            }

            mBinding.flowLayout.addView(textView)
            textView.setOnClickListener {
               // Toast.makeText(requireActivity(), key, Toast.LENGTH_SHORT).show()
                ToastUtils.showShort(requireActivity(),key)
            }
        }

        mBinding.flowLayout.setMaxLines(5) // 设置最大行数
    }

    private fun loadData() {
        mDatas = ArrayList<String>().apply {
            for (i in 0 until 30) {
                add(if (i % 2 == 0) "数据$i" else "数据数据数据$i")
            }
        }
    }




}

